﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using PTTK19.BUS;

namespace PTTK19.GUI
{
    public partial class LapPhieuXuatKho : Form
    {
        public LapPhieuXuatKho()
        {
            InitializeComponent();

        }
        int indexrow = 0;
        int indexrowHuy = -1;
        ArrayList arrIndexRows = new ArrayList();
        //ArrayList arrIndexAdd = new ArrayList();
        private void LapPhieuXuatKho_Load(object sender, EventArgs e)
        {
            LayDanhSachSanPham();
            HienThiMAPXK();
            HienThiDanhSachMaPYC();
            HienThiNgayHienTai();
            dtpNgayGiao.Format = DateTimePickerFormat.Custom;
            dtpNgayGiao.CustomFormat = "yyyy/mm/dd";
        }
        public void LayDanhSachSanPham()
        {
            dgvDanhSachSanPham.DataSource = BUS_SanPham.LayDanhSachSP();
        }
        public void HienThiMAPXK()
        {
            txtMaPhieuXuat.Text = BUS_PhieuXuatKho.LayMaSoPXK().ToString();
        }
        public void HienThiNgayHienTai()
        {
            txtNgayLap.Text = DateTime.Now.ToString("yyyy/mm/dd");
        }
        public void HienThiDanhSachMaPYC()
        {
            cmbMaPYC.DataSource = BUS_PhieuYeuCau.LayDanhSachMaPYC();
            cmbMaPYC.DisplayMember = "MaPhieuYC";
            cmbMaPYC.ValueMember = "MaPhieuYC";
        }


     
        private void dgvDanhSachSanPham_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            indexrow = e.RowIndex;
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (indexrow >= 0)
            {
                DataGridViewRow row = dgvDanhSachSanPhamPXK.Rows[indexrow];
                int Soluongkho = int.Parse(row.Cells[1].Value.ToString());
                int Soluonghuy = int.Parse(row.Cells[1].Value.ToString());

            }
            else
            {
                MessageBox.Show("Hãy chọn sản phẩm.");
            }
        }

        private void dgvDanhSachSanPhamPXK_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            indexrowHuy = e.RowIndex;
        }

       

        private void btnThemVaoPhieu_Click(object sender, EventArgs e)
        {
            if (indexrow >= 0)
            {
                bool b = false;
                DataTable dtSP = new DataTable();
                DataGridViewRow row = dgvDanhSachSanPham.Rows[indexrow];
                int Soluongkho = int.Parse(row.Cells[2].Value.ToString());
                int Soluongcan = int.Parse(txtSoLuong.Text);
                if (Soluongcan > Soluongkho)
                {
                    MessageBox.Show("Số lượng không đủ cung ứng.");
                }
                else if (Soluongcan == 0)
                {
                    MessageBox.Show("Số lượng không được bằng 0.");
                }
                else
                {
                    
                    if (dgvDanhSachSanPhamPXK.Rows.Count > 1)
                    {
                        for (int i = 0; i < dgvDanhSachSanPhamPXK.Rows.Count - 1; i++)
                        {
                            arrIndexRows.Add(dgvDanhSachSanPhamPXK.Rows[i].Cells[0].Value.ToString());                          
                        }

                     
                        for (int i = 0; i < arrIndexRows.Count; i++)
                        {
                            string choi1 = arrIndexRows[i].ToString();
                            string chuoi2 = dgvDanhSachSanPham.Rows[indexrow].Cells[0].Value.ToString();
                            if (arrIndexRows[i].ToString() == dgvDanhSachSanPham.Rows[indexrow].Cells[0].Value.ToString())
                            {
                                b = true;
                                break;
                            }
                        }
                            
                    }
                  
                    if (b == false)
                    {

                        dgvDanhSachSanPhamPXK.Rows.Add(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), txtSoLuong.Text);
                        int Soluongkon = Soluongkho - Soluongcan;
                        dgvDanhSachSanPham.Rows[indexrow].Cells[2].Value = Soluongkon.ToString();
                        //arrIndexRows.Add(dgvDanhSachSanPham.Rows[indexrow].Cells[0].Value);
                       // int count = dgvDanhSachSanPhamPXK.Rows.Count + 1;
                       // arrIndexAdd.Add(count);
                    }
                    else
                    {
                        MessageBox.Show("Sản phẩm đã tồn tại trong phiếu xuất kho.");
                    }
                }
            }
            else
            {
                MessageBox.Show("Hãy chọn sản phẩm.");
            }
        }

        private void btnLapPhieuXuat_Click(object sender, EventArgs e)
        {
            BUS_PhieuXuatKho PXK = new BUS_PhieuXuatKho();
            PXK.MaPXK = int.Parse(txtMaPhieuXuat.Text);
            PXK.MaPYC = int.Parse(cmbMaPYC.SelectedValue.ToString());
            PXK.NgayKham = DateTime.Now;
            PXK.NgayGiao = dtpNgayGiao.Value;
            PXK.TinhTrang = 0;

            ////////
            ArrayList arrSanPham = new ArrayList();
            ArrayList arrSoLuong = new ArrayList();
            for (int i = 0; i < dgvDanhSachSanPhamPXK.Rows.Count; i++)
            {
                arrSanPham.Add(dgvDanhSachSanPhamPXK.Rows[i].Cells[0].Value);
                arrSoLuong.Add(dgvDanhSachSanPhamPXK.Rows[i].Cells[2].Value);
            }
            DataTable dtSP = (DataTable)dgvDanhSachSanPham.DataSource;

            int KetQua = BUS_PhieuXuatKho.ThemPhieuXuat(PXK, arrSanPham, dtSP,arrSoLuong);
            if (KetQua > 0)
            {
                dgvDanhSachSanPham.DataSource = null;
                MessageBox.Show("Lap danh sach thanh cong");
                
                LayDanhSachSanPham();
            }
            else
            {
                MessageBox.Show("Lap danh sach that bai");
            }
        }

        private void btnHuy_Click_1(object sender, EventArgs e)
        {
            try
            {
                int soluongcan = int.Parse(dgvDanhSachSanPhamPXK.Rows[indexrowHuy].Cells[2].Value.ToString());
                string MaSP = dgvDanhSachSanPhamPXK.Rows[indexrowHuy].Cells[0].Value.ToString();
                DataTable dtSP = (DataTable)dgvDanhSachSanPham.DataSource;

                foreach (DataRow dtRow in dtSP.Rows)
                {
                    if (dtRow[0].ToString() == MaSP)
                    {
                        int kq = int.Parse(dtRow[2].ToString()) + soluongcan;
                        dtRow[2] = kq;
                        break;
                    }
                }
               // LayDanhSachSanPham();
                dgvDanhSachSanPham.DataSource = dtSP;
                dgvDanhSachSanPhamPXK.Rows.RemoveAt(indexrowHuy);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Hãy chọn sản phẩm muốn hủy");
            }
           
        }

    }
}
